import { ReactElement } from 'react'
import { render, RenderOptions, RenderResult } from '@testing-library/react'
import { BrowserRouter } from 'react-router-dom'
import { vi } from 'vitest'

// Custom render function that includes providers
const AllTheProviders = ({ children }: { children: React.ReactNode }) => {
  return (
    <BrowserRouter>
      {children}
    </BrowserRouter>
  )
}

const customRender = (
  ui: ReactElement,
  options?: Omit<RenderOptions, 'wrapper'>
): RenderResult => render(ui, { wrapper: AllTheProviders, ...options })

export * from '@testing-library/react'
export { customRender as render }

// Mock data factories
export const createMockPlugin = (overrides = {}) => ({
  name: '@bai-hu-plugin/test',
  manifest: '/api/plugins/test/manifest',
  enabled: true,
  ...overrides,
})

export const createMockManifest = (overrides = {}) => ({
  name: 'Test Plugin',
  version: '1.0.0',
  description: 'A test plugin for testing purposes',
  author: 'Test Author',
  homepage: 'https://example.com',
  tags: ['test', 'example'],
  ...overrides,
})

// Common test helpers
export const waitForLoadingToFinish = () => 
  new Promise(resolve => setTimeout(resolve, 0))

export const mockConsoleError = () => {
  const originalError = console.error
  console.error = vi.fn()
  return () => {
    console.error = originalError
  }
}

export const mockConsoleWarn = () => {
  const originalWarn = console.warn
  console.warn = vi.fn()
  return () => {
    console.warn = originalWarn
  }
}
